<template>
	<view class="classlist">
		<view class="content">
			<view class="item" v-for="item in classList" :key = "item._id">
				<navigator :url="'/pages/perview/perview?id='+item._id" open-type="reLaunch" >
					
					<!-- :url="'/pages/perview/perview?id = ' + item._id" -->
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</navigator>
				
			</view>
			
		</view>
	</view>
</template>

<script setup>
	import {ref} from "vue";
	import{apiGetClassList_ctt} from "@/api/api_ctt.js"
	import {onLoad, onReachBottom, onShareAppMessage, onShareTimeline} from "@dcloudio/uni-app"
	const classList = ref([])
	const queryParams = {pageNum:2}
	let pageName;
	
	onLoad((e) => {
		//console.log(e)
		let {id = null, name = null} = e;
		queryParams.classid = id;
		pageName = name
		console.log(id, name);
		uni.setNavigationBarTitle({
			
			title:name
		})
		getClassList(queryParams.classid)
				.then((res) => {
					console.log('成功:', res);
					classList.value = res.data;  // 虽然你在 getClassList 内也赋值了，这里为了清晰再赋一次
					uni.setStorageSync("storgClassList", classList.value); // ✅ 确保写入 storage 时数据已存在
				})
				.catch((err) => {
					console.error('失败:', err);
				});
				
				
		  
	})
	
	// onReachBottom((e) => {
	// 	getClassList(queryParams.classid)
	// 	  .then((res) => console.log('成功:', res))
	// 	  .catch((err) => console.error('失败:', err))
	// })
	const getClassList = (classid) =>
	  new Promise((resolve, reject) => {
	    //console.log("🔎 发送请求的 classid:", classid)
	    uni.request({
	      url: 'https://xrqr0h0x1w.gzg.sealos.run/get_classlist',
	      method: 'POST',
	      data: { classid },
	      success: (res) => {
	        if (res.statusCode === 200 && res.data.code === 200) {
	          resolve(res.data)
			  classList.value  = res.data.data
	        } else {
	          reject(res.data)
	        }
	      },
	      fail: (err) => reject(err),
	    })
	  })
	
	//分享给好友
	onShareAppMessage(() =>{
			return{
				title:"天天de壁纸"+pageName,
				
				path:"/pages/classList/classList?id="+queryParams.classid+"&name"+pageName,
			}
	
	})
	
	//分享给朋友圈
	onShareTimeline(() => {
				 
			 return{
				 title:"天天de壁纸"+pageName,
				 query:"id="+queryParams.classid+"&name"+pageName,
				 //imageUrl:"https://cttxhy-1360132035.cos.ap-shanghai.myqcloud.com/ctt_logo/ctt_logo_1.jpg",
			 }
	})
		
	
</script>

<style scoped lang="scss">
	.classlist{
		.content{
			display: grid;
			grid-template-columns: repeat(3,1fr);
			gap:5rpx;
			padding:5rpx;
			 .item{
			 				navigator{
			 					image{
			 						width: 100%;
			 						height: 200rpx; /* 设置一个合理的高度 */
			 						object-fit: cover;
			 					}
			 				}
			 			}
			
		}
	}
</style>
